//###############
// FILE : SearchAction.java
// WRITER : Lior Zimmerman liorz06 066462300
// WRITER : Ohad Dan ohad_dan 036767960
// EXERCISE : oop ex3 2008
// DESCRIPTION:
// Does the serach.
//###############
package oop.ex3.fileSearchController;

import java.util.ArrayList;

import oop.ex3.fileSearchModel.SearchEngine.SearchEngine;
import oop.ex3.fileSearchModel.util.FileIterator;
import oop.ex3.fileSearchModel.util.FilterReader;
import oop.ex3.fileSearchModel.util.OutputWriter;
import oop.ex3.fileSearchModel.util.exceptions.ReaderIOException;
import oop.ex3.fileSearchModel.util.exceptions.WriterIOException;
import oop.ex3.fileSearchView.GUIInitializer;
import oop.ex3.fileSearchView.*;

/**
 * Does the search.
 */
public class SearchAction extends Controller {
	 /**
     * Construct a new SearchAction.
     * @param gui The main gui.
     */
	public SearchAction( GUIView gui ) {
		super(gui);
	}
	/**
	 * Initializes the gui and perform the action.
	 */
	public void doAction(){
	  
	    ArrayList<String> list = new ArrayList<String>();
		String where = _gui.getSearchView().txtWorkDirGetText(); 
		SearchEngine searcher = new SearchEngine(where);
		try{
		    String exp = getExpression();
		    list = searcher.search(exp);
		    list.add(0,exp);

		    _gui.setStatus(GUIInitializer.STATUS_READY);
		}
		catch ( Exception e ) {
			_gui.setStatus(e.getMessage());
		}
		listResults(list);

	}
	/**
	 * Gets search expression.
	 * @return str the expression
	 */
	private String getExpression() {
		String textFile = _gui.getSearchView().txtFilterFileGetText();
		String textExp = _gui.getSearchView().txtFilterExpGetText();
		
		if ( !textExp.isEmpty() ) {
			return textExp;
		}else if ( !textFile.isEmpty() ) {
		    String source = getFileExp( textFile );
		    return source;
		}
		return "";
	}
	/**
	 * Return an array with all results.
	 * @param arr Array with all results.
	 */
	private void listResults(ArrayList<String> arr ) {
	    String output="";
        for(String result: arr) {
            output += result + '\n';
        }	  
        SetupTabInterface setupView = _gui.getSetupView();

        
        if( setupView.rdoToFileSelected()
        		|| setupView.rdoToFileScreenSelected()) {
            try{
                OutputWriter writer = new OutputWriter( 
                		setupView.txtGetOutput() );
                writer.writeOutput(output);
            }
            catch(WriterIOException e){
                _gui.setStatus(e.getMessage());
            }
        }
        if ( setupView.rdoToScreenSelected() ||
        		setupView.rdoToFileScreenSelected() ) {
            _gui.getLogView().setLogText(output);
        }
	}
	/**
	 * Return an expression from file.
	 * @param path Path of the file.
	 * @return An expression from file.
	 */
	private String getFileExp( String path ) {
	    try{
	        FilterReader fileReader = new FilterReader(path);
	        FileIterator it = fileReader.iterator();
	        if ( it.hasNext() ) {
	            String filterExp = it.next();
	            fileReader.close();

	            return filterExp;
	        }
            fileReader.close();
	    }
	    catch( ReaderIOException e) {
	        _gui.setStatus(e.getMessage());
	    }
	    return "";
	}
}
